package com.magic233.controller;

import com.magic233.entity.Orders;
import com.magic233.service.OrdersService;
import com.magic233.utils.GetTimeStamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName: OrdersController
 * Description:
 * date: 2021/5/30 0:14
 * author: zouyuan
 */


@Component
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @PostConstruct
    //查询所有工人已完成状态并超过三天的工单，进行自动确认，并自动五星好评
    public void autoConfirmOrder() {
        System.out.println("工单自动确认");
        long nowTime = new Date().getTime();
        System.out.println(nowTime);

        Orders orders = new Orders();
        orders.setType("全部类别");
        orders.setStatus("工人已完成");

        Map<Object, Object> map = ordersService.selectOrders(orders, null, null);

        List<Orders> orders1 = (List<Orders>) map.get("orders");
        String updateTime = null;
        Long updateTimeStamp = null;
        GetTimeStamp getTimeStamp = new GetTimeStamp();
        for (int i = 0; i < orders1.size(); i++) {
            updateTime = orders1.get(i).getUpdatetime();
            updateTimeStamp = getTimeStamp.timeToTimeStamp(updateTime);
            if (nowTime - updateTimeStamp > 86400000 * 3) {
                System.out.println(orders1.get(i));
                System.out.println("超过三天\n\n\n");
                int re = ordersService.systemAutoConfirm(orders1.get(i).getId(), orders1.get(i).getUid(), orders1.get(i).getWid());
                if (re == 1) {
                    System.out.println("-----------系统自动确认成功------------");
                }
            }
        }
    }
}
